<?php
/**
 * Name: AdminUserController.class.php
 * Description:
 * User: DIY
 * Date: 2016/9/8
 * Time: 18:08
 */

namespace Admin\Controller;


use Common\Logic\AdminUserLogic;
use Common\Model\AdminRoleModel;
use Common\Model\AdminUserModel;
use Admin\Library\BootstrapPage;

class AdminUserController extends BaseController{

    public function index(){
        //搜索数据
        $data = I('get.');
        $AdminUserModel = new AdminUserModel();
        $count = $AdminUserModel->getAdminUserCount($data);
        $Page = new BootstrapPage($count,20);
        $limit = $Page->firstRow . ','.$Page->listRows;
        $list['list'] = $AdminUserModel->getAdminUserList($data,$limit);
        $list['page'] = $Page->show();// 分页显示输出
        $this->assign('list',$list);
        $this->display();
    }

    /*
     * 添加新用户
     * */
    public function add(){
        if(IS_POST){
            $AdminUserModel = new AdminUserModel();
            if($AdminUserModel->create()){
                $result = $AdminUserModel->add();
                if($result){
                    $this->success('添加成功',U('Admin/AdminUser/index'));
                }
            }
            $this->error('添加失败'.$AdminUserModel->getError());
        }
        else{
            $AdminRoleModel = new AdminRoleModel();
            $role = $AdminRoleModel->select();
            $this->assign('role',$role);
            $this->display();
        }
    }

    /*
     * 编辑新用户
     * */
    public function edit($id){
        $AdminUserModel = new AdminUserModel();

        if(IS_POST){
            if($AdminUserModel->create()){
                $result = $AdminUserModel->save();
                if($result){
                    $this->success('更新成功',U('Admin/AdminUser/index'));
                }
            }
            $this->error('更新失败'.$AdminUserModel->getError());
        }
        else{
            $AdminRoleModel = new AdminRoleModel();
            $role = $AdminRoleModel->select();
            $user = $AdminUserModel->find($id);
            $this->assign('role',$role);
            $this->assign('user',$user);
            $this->display();
        }
    }

    /*
     * 删除用户
     * */
    public function del($id){
        $AdminUserModel = new AdminUserModel();
        $AdminUserModel->delete($id);
        $this->success('删除成功',U('Admin/AdminUser/index'));
    }

    /*
     * 禁用/启用 用户状态
     * */
    public function change(){
        $AdminUserModel = new AdminUserModel();
        $data['id'] = I('get.id');
        $data['status'] = I('get.status')?1:0;
        $AdminUserModel->save($data);
        $msg = I('get.status')?'禁用成功':'启用成功';
        $this->success($msg,U('Admin/AdminUser/index'));
    }
}